/**
 * 药品库
 */
Lib = cc.Sprite.extend({
	libArr: [],
	openFlag:true,
	doing:false,
	doright:true,
	rm : 5,
	ctor:function(p){
		this._super("#libshow.png");
		p.addChild(this,300);
		this.setTag(TAG_LIB);
		this.setCascadeOpacityEnabled(true);
//		var libbg = new cc.LayerColor(cc.color(0, 0, 0, 200),1280,768);
//		p.addChild(libbg,10, TAG_LIBBG);
//		libbg.setOpacity(0);
		this.loadButton();
	},
	loadButton:function(){
		this.cancel = new ButtonScale(this,"#button/cancel.png",this.action);
		this.sure = new ButtonScale(this,"#button/sure.png",this.action);
		this.cancel.setPosition(cc.p(this.width - 84 - this.sure.width*0.5 - 56 - this.cancel.width,  35 + this.cancel.height*0.5));
		this.sure.setPosition(cc.p(this.width - 84 - this.sure.width*0.5, 35 + this.sure.height*0.5 ));
		this.cancel.setTag(TAG_CANCEL);
		this.sure.setTag(TAG_SURE);
	},
	loadBg:function(tags){
		this.setPosition(cc.p(115 + this.width*0.5, 144 + this.height*0.5))
		this.bgArr = [];
		this.checkArr = [];
		var first = true;
		var firstPos = cc.p( 20-5 , this.height - 26 + 5);
		var prev = null;
		var num = 0;
		var j =1;
		for(var i in tags){
			var bg = new Angel(this,"#uncheck.png", this.callback);
			bg.setTag(TAG_LIB_BG+j);
			j++;
			bg.setAnchorPoint(0,1);
			this.bgArr.push(bg);
			var rel = this.getLibRel(tags[i]);
			var lib = new LibButton(bg,4, rel.tag, rel.img, this.callback,this);
			bg.setCascadeOpacityEnabled(true);
			lib.setCascadeOpacityEnabled(true);
			lib.setPosition(cc.p(bg.width*0.5,bg.height*0.5+10));
			if(first){
				first = false;
				bg.setPosition(firstPos);
			} else {
				this.checkArr.push(bg);
				if(num == 5){
					bg.down(prev, this.rm);
				}else if(num >5){
					bg.left(prev, this.rm);
				}else{
					bg.right(prev, this.rm);
				}				
			}
			prev = bg;
			num++;
		}
	},
	getLibRel:function(tag){
		for(var i in libRelArr){
			if(libRelArr[i].tag == tag){
				return libRelArr[i];
			}
		}
	},
	callback:function(p){
		var pos = this.getPosition(); 
		var action = gg.flow.flow.action;
		switch(p.getTag()){
		case TAG_LIB_AMPULLA:
			cc.log("就是这个！！")
			
			break;	
		
		default:
			break;	
		}
		var bg = p.getParent();
		if(bg.getTag()!==TAG_LIB){				
			if(!bg.check){
				bg.setSpriteFrame("check.png");
				bg.check = true;
				p.check = true;
			}else{
				bg.setSpriteFrame("uncheck.png");
				bg.check = false;
				p.check = false;
				}			
		}else{
			if(!p.check){
				p.setSpriteFrame("check.png");
				p.check = true;
			}else{
				p.setSpriteFrame("uncheck.png");
				p.check = false;
			}	
		}
	
	},
	action:function(p){
		switch(p.getTag()){
		case TAG_SURE:
			if(this.doright){
				for(var i  in this.checkArr){					 
					if(this.checkArr[i].check  ){
						this.showTip = new ShowTip("仪器选择错误！",cc.p(gg.width*0.4,220),true);
							ll.tip.mdScore(-3);
							return;
						}
				}
				if(!this.bgArr[0].check){
					if(ll.run.getChildByTag(TAG_SHOW)==null){
						this.showTip = new ShowTip("请选择仪器！",cc.p(gg.width*0.4,220),true);	
					}	
				}else{
					gg.flow.next();
					this.doright=false;		
					this.close();
					var ampulla = ll.run.ampulla;
					ampulla.setVisible(true);
					ll.tip.mdScore(10);
				}
				
			}else{
				this.showTip = new ShowTip("仪器已选择，请点击关闭并返回实验！",cc.p(gg.width*0.4-50,200),true);
			}
			
			break;
		case TAG_CANCEL:
			this.close();
			break;
		}	
	},
	isOpen:function(){
		return this.openFlag; 
	},
	open:function(){
		if(this.openFlag || this.doing){
			return;
		}
//		var libbg = this.getParent().getChildByTag(TAG_LIBBG);
//		libbg.runAction(cc.fadeTo(0.4, 200));
		this.doing = true;
		var fadein = cc.fadeIn(0.4);
		var func = cc.callFunc(function(){
			this.openFlag = true;
			this.doing = false;
			var tag = gg.flow.flow.tag;
			 if(tag instanceof Array){
				 if(TAG_LIB_MIN < tag[1]){
					 // 显示箭头
					 gg.flow.location();
				 }
			 }
		}, this);
		var seq = cc.sequence(fadein,func);
		this.runAction(seq);
	},
	close:function(){
		if(!this.openFlag || this.doing){
			return;
		}
//		var libbg = this.getParent().getChildByTag(TAG_LIBBG);
//		libbg.runAction(cc.fadeTo(0.4, 0));
		this.doing = true;
		var fadeout = cc.fadeOut(0.4);
		var func = cc.callFunc(function(){
			this.openFlag = false;
			this.doing = false;
			var tag = gg.flow.flow.tag;
			if(tag instanceof Array){
				if(TAG_LIB_MIN < tag[1]){
					// 隐藏箭头
					//ll.tip.arr.out();
					//ll.tip.arr.setPosition(gg.width-45,455);
					ll.tip.arr.pos(ll.tool.getChildByTag(TAG_BUTTON_LIB));
				}
			}
		}, this);
		var seq = cc.sequence(fadeout,func);
		this.runAction(seq);
	}
});
TAG_LIB_MIN = 30000;

TAG_LIB_AMPULLA = 30001;
TAG_LIB_BEAKER=30002;
TAG_LIB_BOTTLE=30003;
TAG_LIB_CONICAL=30004;
TAG_LIB_CYLINDER=30005;
TAG_LIB_NET=30006;
TAG_LIB_PIPE=30007;
TAG_LIB_ROCK=30008;
TAG_LIB_TESTTUBE=30009;
TAG_LIB_VOLUMETRIC=30010;

TAG_LIB_BG=30050;
TAG_LIB_BG1=30051;
TAG_LIB_BG2=30052;
TAG_LIB_BG3=30053;
TAG_LIB_BG4=30054;
TAG_LIB_BG5=30055;
TAG_LIB_BG6=30056;
TAG_LIB_BG7=30057;
TAG_LIB_BG8=30058;
TAG_LIB_BG9=30059;
TAG_LIB_BG10=30060;

TAG_CANCEL=30081;
TAG_SURE=30082;

libRelArr = [
     {tag:TAG_LIB_MIN, name:""},  
     {tag:TAG_LIB_AMPULLA, name:"玻璃瓶",img:"#ampulla.png"},
     {tag:TAG_LIB_BEAKER,name:"烧杯",img:"#beaker.png"},
     {tag:TAG_LIB_BOTTLE,name:"塑料瓶",img:"#bottle.png"},
     {tag:TAG_LIB_CONICAL,name:"锥形瓶",img:"#conical.png"},
     {tag:TAG_LIB_CYLINDER,name:"量筒",img:"#cylinder.png"},
     {tag:TAG_LIB_NET, name:"石棉网",img:"#net.png"},
     {tag:TAG_LIB_PIPE,name:"冷凝管",img:"#pipe.png"},
     {tag:TAG_LIB_ROCK,name:"沸石",img:"#rock.png"},
     {tag:TAG_LIB_TESTTUBE,name:"试管",img:"#testTube.png"},
     {tag:TAG_LIB_VOLUMETRIC,name:"烧瓶",img:"#volumetric.png"},
     ];
